package com.mzy.linear_struct.hot100.链表;

import com.sun.org.apache.bcel.internal.generic.RET;
import com.sun.org.apache.bcel.internal.generic.Visitor;
import com.sun.org.apache.bcel.internal.util.ByteSequence;

/**
 * @Author: mizy3
 * @Date: 2024/08/06/下午4:40
 * @Description:
 */
public class Pro24两两交换链表中的节点 {

    /*
    public static ListNode swapPairs(ListNode head) {
        ListNode cur = new ListNode(0);
        cur.next = head;

        head = cur;
        while (cur != null && cur.next != null && cur.next.next != null) {
            ListNode next = cur.next;
            ListNode nextTwo = cur.next.next;
            next.next = nextTwo.next;
            nextTwo.next = next;
            cur.next = nextTwo;
            cur = next;
        }

        return head.next;
    }
    */
    public static ListNode swapPairs(ListNode head) {
        ListNode prev = new ListNode(0, head);
        ListNode dummy = prev;
        while (prev != null && prev.next != null && prev.next.next != null) {
            ListNode next = prev.next;
            ListNode nextNext = prev.next.next;
            next.next = nextNext.next;
            nextNext.next = next;
            prev.next = nextNext;
            prev = nextNext;
        }

        return dummy;
    }

    public static void main(String[] args) {
        ListNode listNode = swapPairs(new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4)))));
        while (listNode != null) {
            System.out.print(listNode.val);
            listNode = listNode.next;
        }
    }
}















